package com.hnu.highlynewapp.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hnu.highlynewapp.vo.DurationFieldVo;
import io.swagger.annotations.*;
import com.hnu.highlynewapp.exception.BizCodeEnum;
import com.hnu.highlynewapp.utils.ValidationUtils;
import com.hnu.highlynewapp.vo.PublishTechAchVo;
import com.hnu.highlynewapp.vo.PublishTechNeedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hnu.highlynewapp.entity.TechnologyNeedEntity;
import com.hnu.highlynewapp.service.TechnologyNeedService;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.R;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 技术需求
 *
 * @author hnu_worker
 * @email 825963704@qq.com
 * @date 2021-01-03 10:35:56
 */
@Api(tags = "技术api管理")
@RestController
@RequestMapping("/technologyneed")
public class TechnologyNeedController {
    @Autowired
    private TechnologyNeedService technologyNeedService;

    /**
     * 发布技术需求
     */
    @ApiOperation("发布技术需求")
    @PostMapping("/publish")
    public R publish(@Validated @RequestBody PublishTechNeedVo vo, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, String> map = ValidationUtils.getErrors(result);
            return R.error(BizCodeEnum.INPUT_INVALID.getCode(), BizCodeEnum.INPUT_INVALID.getMessage()).put("data",map);
        } else {
            technologyNeedService.publish(vo);
            return R.ok();
        }
    }

    /**
     * 根据企业ID查询所有的技术需求
     * @param businessID
     * @return
     */
    @ApiOperation("根据企业ID查询所有的技术需求")
    @GetMapping("/listByBusinessID/{businessID}")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success",response = TechnologyNeedEntity.class)
    })
    public R listByBusinessID(@ApiParam(value = "企业id, 即用户登录时获取到的gid",required = true) @PathVariable("businessID") long businessID) {
        List<TechnologyNeedEntity> technologyNeedEntities = technologyNeedService.listByBusinessID(businessID);
        return R.ok().put("data",technologyNeedEntities);
    }

    /**
     * 根据技术需求id显示技术需求详细信息
     * @param technologyNeedId
     * @return
     */
    @ApiOperation("根据技术需求id查询技术需求详细信息")
    @GetMapping("/details/{technologyNeedId}")
    @ApiResponses({
            @ApiResponse(code = 10003, message = "查询的信息不存在"),
            @ApiResponse(code = 0,message = "success",response = TechnologyNeedEntity.class)
    })
    public R details(@PathVariable("technologyNeedId") @ApiParam(value = "技术需求id",required = true) Long technologyNeedId){
        TechnologyNeedEntity technologyNeed = technologyNeedService.getById(technologyNeedId);
        if (technologyNeed == null) {
            return R.error(BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getCode(),BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getMessage());
        }

        return R.ok().put("technologyNeed", technologyNeed);
    }

    /**
     * 列表
     */
    @ApiIgnore
    @RequestMapping("/list")
    //@RequiresPermissions("highlynewapp:technologyneed:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = technologyNeedService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiIgnore
    @RequestMapping("/info/{technologyNeedId}")
    //@RequiresPermissions("highlynewapp:technologyneed:info")
    public R info(@PathVariable("technologyNeedId") Long technologyNeedId){
		TechnologyNeedEntity technologyNeed = technologyNeedService.getById(technologyNeedId);

        return R.ok().put("technologyNeed", technologyNeed);
    }

    /**
     * 保存
     */
    @ApiIgnore
    @RequestMapping("/save")
    //@RequiresPermissions("highlynewapp:technologyneed:save")
    public R save(@RequestBody TechnologyNeedEntity technologyNeed){
		technologyNeedService.save(technologyNeed);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiIgnore
    @RequestMapping("/update")
    //@RequiresPermissions("highlynewapp:technologyneed:update")
    public R update(@RequestBody TechnologyNeedEntity technologyNeed){
		technologyNeedService.updateById(technologyNeed);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiIgnore
    @RequestMapping("/delete")
    //@RequiresPermissions("highlynewapp:technologyneed:delete")
    public R delete(@RequestBody Long[] technologyNeedIds){
		technologyNeedService.removeByIds(Arrays.asList(technologyNeedIds));

        return R.ok();
    }

    /**
     * 根据时间和行业领域检索
     * @param vo
     * @return
     */
    @PostMapping("/listByCondition")
    @ApiOperation("根据时间和行业领域查询技术需求")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success",response = TechnologyNeedEntity.class)
    })
    public R listByCondition(@RequestBody DurationFieldVo vo) {
        return R.ok().put("data",technologyNeedService.listByCondition(vo.getDuration(), vo.getField()));
    }


}
